package com.itheima.stock;

import lombok.Data;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.Cookie;
import java.util.List;

/**
 * @author by jojo
 * @Date 2022/1/14
 * @Description
 * 测试RestTemplate
 */
@SpringBootTest
public class RestTemplateTestPost {
    @Autowired
    private RestTemplate restTemplate;

    /*
    POST请求模拟form表单访问外部接口
     */
    @Test
    public void test01(){
        String url="http://localhost:6666/account/addAccount";
        //设置请求头，指定请求数据方式
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-type","application/x-www-form-urlencoded");
        //组装模拟form表单提交数据
        LinkedMultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
        map.add("id",6);
        map.add("userName","蒙奇·D·路飞");
        map.add("address","风车村");
        HttpEntity<LinkedMultiValueMap<String, Object>> entity = new HttpEntity<>(map, headers);
             /*
            参数1：请求url地址
            参数2：请求方式 POST
            参数3：请求体对象，携带了请求头和请求体相关的参数
            参数4：响应数据类型
         */
        ResponseEntity<Account> exchange = restTemplate.exchange(url, HttpMethod.POST, entity, Account.class);
        Account body = exchange.getBody();
        System.out.println(body);
    }
/*
POST请求发送JSON数据
 */
    @Test
    public void test02(){
        String url="http://localhost:6666/account/updateAccount";
        //设置请求头的请求参数类型
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-type","application/json; charset=utf-8");
        //组装json格式数据
        String jsonReq="{\"address\":\"上海\",\"id\":\"1\",\"userName\":\"zhangsan\"}";
        //构建请求对象
        HttpEntity<String> entity = new HttpEntity<>(jsonReq, headers);
        /*
            发送数据
            参数1：请求url地址
            参数2：请求方式
            参数3：请求体对象，携带了请求头和请求体相关的参数
            参数4：响应数据类型
         */
        ResponseEntity<Account> exchange = restTemplate.exchange(url, HttpMethod.POST, entity, Account.class);
        Account body = exchange.getBody();
        System.out.println(body);
    }

    /*
    6）获取接口响应的cookie数据
     */
    @Test
    public void text03(){
        String url="http://localhost:6666/account/getCookie";
        ResponseEntity<String> forEntity = restTemplate.getForEntity(url, String.class);
        //获取cookie
        List<String> cooks = forEntity.getHeaders().get("Set-Cookie");
        //获取响应数据
        String body = forEntity.getBody();
        System.out.println(body);
        System.out.println(cooks);
    }







    @Data
    public static class Account {

        private Integer id;

        private String userName;

        private String address;

    }
}
